/* Team 5
 * James McQueen
 * Corwyn Simpson
 * May 1 ish, 2012
 */
package spider.models;

import java.net.URL;

/**
 * A class encapsulating a single data point from the parsing of a web page.
 * Contains a source URL, a keyword, and the number of times that the keyword
 * appeared on the web page.
 * @author James McQueen
 */
public class Data
{
    /**
     * The source URL, where the web page was retrieved from.
     */
    private FastURL my_source;
    /**
     * The keyword that this data point represents.
     */
    private String my_keyword;
    /**
     * The number of times that the keyword appeared on the web page.
     */
    private int my_count;
    
    /**
     * Creates a new data point with the given source, keyword, and count.
     * @param the_source where this data point came from.
     * @param the_keyword Which keyword that this data point represents.
     * @param the_count How many times that the keyword appeared.
     */
    public Data(final URL the_source, final String the_keyword, final int the_count)
    {
        my_source = new FastURL(the_source);
        my_keyword = the_keyword;
        my_count = the_count;
    }
    
    /**
     * Returns the source URL.
     */
    public URL getUrl()
    {
        return my_source.getURL();
    }

    /**
     * Returns the keyword that this data point represents.
     */
    public String getData()
    {
        return my_keyword;
    }
    
    /**
     * Returns the number of times the keyword appeared on the source page.
     */
    public int getMatches()
    {
        return my_count;
    }
    
    @Override
    public String toString()
    {
        String word = "[my_source: " + my_source.getURL() + "  my_keyword: " + my_keyword
                + "  my_count: " + my_count + ']';      
        return word;
    }
    
//As we are using a HashSet to store these, it is important to implement both
//hashCode() and equals()
    @Override
    public int hashCode()
    {
        int hash = my_source.hashCode();
        hash = hash * 27 + my_keyword.hashCode();
        return hash;
    }
    
    /**
     * Returns true if this object is equal to the_other. This is true if: <ul>
     * <li>the_other is not null and is an instance of Data.</li>
     * <li>both the keyword and the source for this and the_other are the same</li>
     * </ul>
     */
    @Override
    public boolean equals(final Object the_other)
    {
        if (the_other == null || !(the_other instanceof Data))
            return false;
        else
        {
            Data other_data = (Data) the_other;
            return (this.my_keyword.equals(other_data.my_keyword) && 
                    this.my_source.equals(other_data.my_source));
        }
    }
}
